package com.xfcy.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xfcy.blog.common.R;
import com.xfcy.blog.common.constant.Constant;
import com.xfcy.blog.common.constant.SqlConstant;
import com.xfcy.blog.dto.CategoryDTO;
import com.xfcy.blog.mapper.ArticleMapper;
import com.xfcy.blog.mapper.CategoryMapper;
import com.xfcy.blog.entity.Category;
import com.xfcy.blog.mapper.TagsMapper;
import com.xfcy.blog.service.ArticleService;
import com.xfcy.blog.service.CategoryService;
import com.xfcy.blog.utils.BeanCopyUtil;
import com.xfcy.blog.vo.CategoryVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 博客分类表(Category)表服务实现类
 *
 * @author 晓风残月Lx
 * @since 2023-04-09 11:03:05
 */
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Resource
    private ArticleService articleService;

    @Resource
    private ArticleMapper articleMapper;

    @Resource
    private TagsMapper tagsMapper;

    /**
     * 后台分类列表
     *
     * @param pageNo
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public R listRecord(Integer pageNo, Integer pageSize, String name) {
        Page<CategoryVO> categoryVOPage = baseMapper.selectListRecord(new Page<>(pageNo, pageSize), name);
        return R.success(categoryVOPage);
    }

    /**
     * 后台分类删除
     *
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R deleteCategoryId(Long id) {
        baseMapper.deleteById(id);
        deleteArticleByCategoryId(Collections.singletonList(id));
        return R.success();
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R deleteBatchCategoryId(List<Long> ids) {
        deleteArticleByCategoryId(ids);
        int rows = baseMapper.deleteBatchIds(ids);
        return rows > 0 ? R.success(): R.error("批量删除分类失败");
    }

    /**
     * 置顶或取消
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R top(Long id) {
        Category category = baseMapper.selectOne(new QueryWrapper<Category>().select(SqlConstant.SORT).eq(SqlConstant.ID, id));
        if (Objects.equals(category.getSort(), Constant.ZERO)) {
            baseMapper.updateSort(id, Constant.ONE);
        } else {
            baseMapper.updateSort(id, Constant.ZERO);
        }
        return R.success();
    }

    /**
     * 添加分类
     * @param categoryDTO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R addCategory(CategoryDTO categoryDTO) {
        Category category = BeanCopyUtil.copyObject(categoryDTO, Category.class);
        int insert = baseMapper.insert(category);
        return insert > 0 ? R.success() : R.error();
    }

    /**
     * 获取分类详细
     * @param id
     * @return
     */
    @Override
    public R getCategoryById(Long id) {
        Category category = baseMapper.selectOne(new QueryWrapper<Category>().select(SqlConstant.ID, SqlConstant.NAME, SqlConstant.SORT, SqlConstant.CLICK_VOLUME).eq(SqlConstant.ID, id));
        CategoryVO categoryVO = BeanCopyUtil.copyObject(category, CategoryVO.class);
        return R.success(categoryVO);
    }

    /**
     * 更新分类
     * @param categoryDTO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R updateCategory(CategoryDTO categoryDTO) {
        Category category = BeanCopyUtil.copyObject(categoryDTO, Category.class);
        int rows = baseMapper.updateById(category);
        return rows > 0 ? R.success() : R.error();
    }


    // Web 方法

    /**
     * 前台分类列表
     *
     * @return
     */
    @Override
    public R categoryList() {
        List<CategoryVO> list = baseMapper.selectAll();
        return R.success(list);
    }


    // 自定义方法
    private void deleteArticleByCategoryId(List<Long> ids) {
        articleMapper.deleteByCategoryId(ids);
    }
}


